/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package gui;

import interfaces.EditPanel;

import java.awt.Dimension;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import database.DBConnectionFactory;

/**
 * Settings panel for sciences
 * 
 * 
 */
public class SciencesPanel extends JPanel implements EditPanel {
   private static final long serialVersionUID = 4228896307757033356L;
   private JTextField name;
   private JTextField abbreviation;
   private JTextField formula;
   private JTextField result;
   private JTextField syntax;
   private JTextField angel;
   private String itemName;
   private final DialogPanel parent;

   public SciencesPanel(final DialogPanel parent) {
	  super();
	  this.parent = parent;
	  initialize();
   }

   @Override
   public void checkInputs() {
	  if (name.getText().equals("") || abbreviation.getText().equals("") || formula.getText().equals("")
			|| result.getText().equals("") || syntax.getText().equals("") || angel.getText().equals("")) {
		 parent.setSaveButtonEnabled(false);
	  } else {
		 parent.setSaveButtonEnabled(true);
	  }
   }

   @Override
   public boolean delete() {
	  if (name.getText().equals(""))
		 return false;
	  PreparedStatement ps = null;
	  boolean success = false;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("DELETE FROM science WHERE name = ?");
		 ps.setString(1, itemName);
		 ps.executeUpdate();
		 success = true;
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return success;
   }

   @Override
   public String getItemName() {
	  return itemName;
   }

   /**
    * Initializes the components
    */
   private void initialize() {
	  setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
	  name = new JTextField();
	  name.setPreferredSize(new Dimension(300, 20));
	  name.getDocument().addDocumentListener(new MyDocumentListener(this));
	  abbreviation = new JTextField();
	  abbreviation.setPreferredSize(new Dimension(300, 20));
	  abbreviation.getDocument().addDocumentListener(new MyDocumentListener(this));
	  formula = new JTextField();
	  formula.setPreferredSize(new Dimension(300, 20));
	  formula.getDocument().addDocumentListener(new MyDocumentListener(this));
	  result = new JTextField();
	  result.setPreferredSize(new Dimension(300, 20));
	  result.getDocument().addDocumentListener(new MyDocumentListener(this));
	  syntax = new JTextField();
	  syntax.setPreferredSize(new Dimension(300, 20));
	  syntax.getDocument().addDocumentListener(new MyDocumentListener(this));
	  angel = new JTextField();
	  angel.setPreferredSize(new Dimension(300, 20));
	  angel.getDocument().addDocumentListener(new MyDocumentListener(this));

	  this.add(new CustomPanel(new CustomLabel("Name", 80, 20), name));
	  this.add(new CustomPanel(new CustomLabel("Abbreviation", 80, 20), abbreviation));
	  this.add(new CustomPanel(new CustomLabel("Formula", 80, 20), formula));
	  this.add(new CustomPanel(new CustomLabel("Result", 80, 20), result));
	  this.add(new CustomPanel(new CustomLabel("Syntax", 80, 20), syntax));
	  this.add(new CustomPanel(new CustomLabel("Angel Name", 80, 20), angel));
   }

   @Override
   public JPanel load(final String item) {
	  itemName = item;
	  removeAll();
	  validate();
	  initialize();
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT * FROM science WHERE name = ?");
		 ps.setString(1, item);
		 rs = ps.executeQuery();

		 if (rs.next()) {
			name.setText(rs.getString("name"));
			abbreviation.setText(rs.getString("abbreviation"));
			formula.setText(rs.getString("formula"));
			result.setText(rs.getString("result"));
			syntax.setText(rs.getString("syntax"));
			angel.setText(rs.getString("angel"));
		 } else {
			name.setText(item);
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return this;
   }

   @Override
   public boolean save() {
	  if (name.getText().equals("") || abbreviation.getText().equals("") || formula.getText().equals("")
			|| result.getText().equals("") || syntax.getText().equals("") || angel.getText().equals(""))
		 return false;
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  boolean success = false;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT * FROM science WHERE name = ?");
		 ps.setString(1, itemName);
		 rs = ps.executeQuery();

		 if (rs.next()) {
			try {
			   ps.close();
			} catch (final Exception e) {
			}

			ps = DBConnectionFactory
				  .getConnectionHandler()
				  .getConnection()
				  .prepareStatement(
						"UPDATE science SET name = ?,abbreviation = ?,formula = ?,result = ?,"
							  + "syntax = ?,angel = ? WHERE name = ?");
			ps.setString(1, name.getText());
			ps.setString(2, abbreviation.getText());
			ps.setString(3, formula.getText());
			ps.setString(4, result.getText());
			ps.setString(5, syntax.getText());
			ps.setString(6, angel.getText());
			ps.setString(7, itemName);
			ps.executeUpdate();
		 } else {
			try {
			   ps.close();
			} catch (final Exception e) {
			}

			ps = DBConnectionFactory.getConnectionHandler().getConnection()
				  .prepareStatement("INSERT INTO science VALUES(?,?,?,?,?,?)");
			ps.setString(1, name.getText());
			ps.setString(2, abbreviation.getText());
			ps.setString(3, formula.getText());
			ps.setString(4, result.getText());
			ps.setString(5, syntax.getText());
			ps.setString(6, angel.getText());
			ps.executeUpdate();
		 }
		 itemName = name.getText();
		 success = true;
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return success;
   }
}
